package com.ruoyi.bizsys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.bizsys.domain.LoanIou;
import com.ruoyi.bizsys.domain.ProductInfo;
import com.ruoyi.bizsys.domain.RepaymentOrder;
import com.ruoyi.bizsys.mapper.LoanOffsetMapper;
import com.ruoyi.bizsys.mapper.RepaymentOrderMapper;
import com.ruoyi.bizsys.service.IRepaymentOrderService;
import com.ruoyi.bizsys.utils.BizConstants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 还款管理Service业务层处理
 *
 * @author african-loan
 * @date 2021-12-30
 */
@Slf4j
@Service
public class RepaymentOrderServiceImpl extends ServiceImpl<RepaymentOrderMapper, RepaymentOrder> implements IRepaymentOrderService {

    @Autowired
    private RepaymentOrderMapper repaymentOrderMapper;

    @Autowired
    private LoanOffsetMapper loanOffsetMapper;

    /**
     * 查询还款管理
     *
     * @param id 还款管理ID
     * @return 还款管理
     */
    @Override
    public RepaymentOrder selectRepaymentOrderById(Long id) {
        return this.getById(id);
    }

    /**
     * 查询还款管理列表
     *
     * @param repaymentOrder 还款管理
     * @return 还款管理
     */
    @Override
    public List<RepaymentOrder> selectRepaymentOrderList(RepaymentOrder repaymentOrder) {
        return this.list(new QueryWrapper<>(repaymentOrder).last("order by id desc"));
    }

    /**
     * 后台用
     * @param repaymentOrder
     * @return
     */
    @Override
    public List<RepaymentOrder> getList(RepaymentOrder repaymentOrder) {
        if(repaymentOrder.getParams().entrySet().size() > 0){
            return this.list(new QueryWrapper<>(repaymentOrder).ge("create_time",repaymentOrder.getParams().get("beginTime")).lt("create_time",repaymentOrder.getParams().get("endTime")).last("order by id desc"));
        }
        return this.list(new QueryWrapper<>(repaymentOrder).last("order by id desc"));

    }

    /**
     * 新增还款管理
     *
     * @param repaymentOrder 还款管理
     * @return 结果
     */
    @Override
    public int insertRepaymentOrder(RepaymentOrder repaymentOrder) {
        Date nowTime = new Date();
        repaymentOrder.setCreateTime(nowTime);
        repaymentOrder.setUpdateTime(nowTime);
        return baseMapper.insert(repaymentOrder);
    }

    /**
     * 修改还款管理
     *
     * @param repaymentOrder 还款管理
     * @return 结果
     */
    @Override
    public int updateRepaymentOrder(RepaymentOrder repaymentOrder) {
        repaymentOrder.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(repaymentOrder);
    }

    /**
     * 批量删除还款管理
     *
     * @param ids 需要删除的还款管理ID
     * @return 结果
     */
    @Override
    public int deleteRepaymentOrderByIds(Long[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除还款管理信息
     *
     * @param id 还款管理ID
     * @return 结果
     */
    @Override
    public int deleteRepaymentOrderById(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public int getWithholdCardCount(String custNo) {
        return baseMapper.getWithholdCardCount(custNo);
    }

    /**
     * state = 1
     *
     * @param repaymentNo
     * @return
     */
    @Override
    public RepaymentOrder getDetail(String repaymentNo) {
        return baseMapper.getDetail(repaymentNo);
    }

    /**
     * repay_status =5 state =1 payTransSerialNo
     *
     * @param serialNo
     * @return
     */
    @Override
    public RepaymentOrder getDetailByPayTransSerialNo(String serialNo) {
        return baseMapper.getDetailByPayTransSerialNo(serialNo);
    }

    @Override
    public RepaymentOrder getDetailByPayTransSerialNoStatusSucc(String serialNo) {
        return baseMapper.getDetailByPayTransSerialNoStatusSucc(serialNo);
    }

    @Override
    public void updateRepayStatusToFaild(String createTime) {
        baseMapper.updateRepayStatusToFaild(createTime);
    }

    @Override
    public List<ProductInfo> selectCreditAccountProduct(String custNo) {
        return repaymentOrderMapper.selectCreditAccountProduct(custNo);
    }

    @Override
    @Transactional
    public void updateRepaymentOrderAndOffsetFaild(RepaymentOrder repaymentOrder) {
        loanOffsetMapper.updateFaildeByrepaymentNo(repaymentOrder.getRepaymentNo());
        repaymentOrder.setRemark("手动，解冻钱包");
        repaymentOrder.setRepayStatus(BizConstants.REPAYMENT_ORDER_STATUS_FAIL);
        repaymentOrderMapper.updateById(repaymentOrder);
    }

    @Override
    public RepaymentOrder createRepaymentOrderToWait(LoanIou loanIou,String actionType,String paymentReference) {
        RepaymentOrder repaymentOrder = new RepaymentOrder();
        repaymentOrder.setRepaymentNo(IdUtils.reqId());
        repaymentOrder.setCustNo(loanIou.getCustNo());
        repaymentOrder.setLoanSerialNo(loanIou.getLoanSerialNo());
        repaymentOrder.setActionType(actionType);
        repaymentOrder.setRepaymentAmt(LoanIou.calculationOverdueAmount(loanIou).setScale(0, BigDecimal.ROUND_UP));
        repaymentOrder.setShouldRepaymentAmt(LoanIou.calculationOverdueAmount(loanIou).setScale(0, BigDecimal.ROUND_UP));

        BigDecimal reduceAmt = loanIou.getReduceAmt() == null ? BigDecimal.ZERO : loanIou.getReduceAmt();
        repaymentOrder.setReduceFee(reduceAmt);

        repaymentOrder.setLoanPrincipal(loanIou.getLoanPrincipal());
        repaymentOrder.setRepaidPrincipal(loanIou.getRepaidPrincipal());
        repaymentOrder.setServiceFee(loanIou.getServiceFee());
        repaymentOrder.setRepaidServiceFee(loanIou.getRepaidServiceFee());
        repaymentOrder.setLoanInterest(loanIou.getLoanInterest());
        repaymentOrder.setRepaidInterest(loanIou.getRepaidInterest());
        repaymentOrder.setLateFee(loanIou.getLateFee());
        repaymentOrder.setRepaidLateFee(loanIou.getRepaidLateFee());
        repaymentOrder.setRepayStatus(BizConstants.REPAYMENT_ORDER_STATUS_WAIT);
        repaymentOrder.setPayTransSerialNo(paymentReference);
        repaymentOrder.setState(UserConstants.BUSINESS_NORMAL);
        Date nowTime = new Date();
        // 时间会被改变
        repaymentOrder.setPayTime(nowTime);
        repaymentOrder.setCreateTime(nowTime);
        repaymentOrder.setUpdateTime(nowTime);
        repaymentOrderMapper.insert(repaymentOrder);
        return repaymentOrder;
    }

    @Override
    public void updateRepayStatusToFaildByPayTransSerialNo(String walletNo) {
        repaymentOrderMapper.updateRepayStatusToFaildByPayTransSerialNo(walletNo);
    }

    /**
     * 参数对象
     *
     * @param repaymentOrder
     * @return
     */
    @Override
    public RepaymentOrder getDetail(RepaymentOrder repaymentOrder) {
        return baseMapper.getDetailByVo(repaymentOrder);
    }

}
